package com.itvkt.mybatis.main;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.itvkt.mybatis.mapper.RoleMapper;
import com.itvkt.mybatis.pojo.Role;
import com.itvkt.mybatis.utils.SqlSessionFactoryUtils;

public class Main {

	public static void main(String[] args) {
		Logger log = Logger.getLogger(Main.class);
		SqlSession sqlSession = null;

		try {
			sqlSession = SqlSessionFactoryUtils.opSqlSession();
			RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
			// 分页
			RowBounds rowBounds = new RowBounds(0, 20);
			List<Role> roleList = roleMapper.findByRowBounds("role_name", "note", rowBounds);
			System.out.println(roleList.size());
			
			// 查询
			Role role = roleMapper.getRole(1L);
			log.info(role.getId() + "---" + role.getRoleName() + "---" + role.getNote());
			
			// 插入
			role.setNote("333");
			role.setRoleName("111111");
			role.setId(13L);
			roleMapper.updateRole(role);
			
			// 删除
//			roleMapper.deleteRole(8L);
			
			// 提交
			sqlSession.commit();
			sqlSession.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
	}

}
